package ms.shop.service;

import com.github.pagehelper.PageHelper;
import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.bean.BeanUtil;
import ms.common.util.string.StringUtil;
import ms.shop.bean.ClassifyParamBean;
import ms.shop.bean.ClassifyQueryParamBean;
import ms.shop.config.ResponseCode;
import ms.shop.dao.ClassifyDAO;
import ms.shop.dao.GoodDAO;
import ms.shop.entity.Classify;
import ms.shop.entity.Good;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Project Name: ms
 * File Name: ClassifyService
 * Package Name: ms.shop.service
 * Date: 2018/5/31  10:55
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class ClassifyService {

    @Autowired
    private ClassifyDAO classifyDAO;

    @Autowired
    private GoodDAO goodDAO;

    /**
     * 分页查询
     * @param classifyQueryParamBean
     * @param key
     * @return
     */
    @Cacheable(value = "queryClassifyPage", key = "'queryClassifyPage' + #classifyQueryParamBean.pageNo", condition="#key==''" )
    public Page<Classify> query(ClassifyQueryParamBean classifyQueryParamBean, String key) {
        //使用pageHelper插件进行分页查询
        com.github.pagehelper.Page page = PageHelper.startPage(classifyQueryParamBean.getPageNo(), classifyQueryParamBean.getPageSize());
        List<Classify> classifys = classifyDAO.queryClassifys(classifyQueryParamBean.getName(), classifyQueryParamBean.getPid(), classifyQueryParamBean.getStatus());

        return new Page(page.getPageNum(), page.getPageSize(), (int)page.getTotal(), classifys);
    }

    /**
     * 分类列表
     * @param classifyQueryParamBean
     * @param key
     * @return
     */
    @Cacheable(value = "queryClassifyList", key = "'queryClassifyList' + #classifyQueryParamBean.pid + '-' + #classifyQueryParamBean.getStatus()", condition="#key==''" )
    public List<Classify> list(ClassifyQueryParamBean classifyQueryParamBean, String key) {
        return classifyDAO.queryClassifys("", classifyQueryParamBean.getPid(), classifyQueryParamBean.getStatus());
    }

    /**
     * 新增
     * @param classifyParamBean
     */
    @CacheEvict(value= {"queryClassifyPage", "queryClassifyList"},allEntries=true)// 清空 user_detail queryPage 缓存
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void create(ClassifyParamBean classifyParamBean) {
        //新增
        Classify classify = BeanUtil.copy(classifyParamBean, Classify.class);
        classify.setStatus(classifyParamBean.getStatus());
        classify.setCreateTime(System.currentTimeMillis());
        classify.setUpdateTime(System.currentTimeMillis());

        classifyDAO.insert(classify);
    }

    /**
     * 修改
     * @param classifyParamBean
     */
    @Caching(put = @CachePut(value = "classify_detail", key = "'classify_detail' + #classifyParamBean.id"), evict = { @CacheEvict(value = {"queryClassifyPage", "queryClassifyList"}, allEntries = true)})
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void update(ClassifyParamBean classifyParamBean) {

        Classify currentClassify = classifyDAO.selectByPrimaryKey(classifyParamBean.getId());
        if(StringUtil.isEmpty(currentClassify)) {
            throw new BizException(ResponseCode.CLASSIFY_UN_EXIST, "分类信息不存在");
        }

        //修改
        Classify classify = BeanUtil.copy(classifyParamBean, Classify.class);
        if(StringUtil.isEmpty(classify.getImg())) {
            classify.setImg(currentClassify.getImg());
        }
        classify.setStatus(null);
        classify.setCreateTime(null);
        classify.setUpdateTime(System.currentTimeMillis());

        classifyDAO.updateByPrimaryKeySelective(classify);
    }

    /**
     * 启用
     * @param id
     */
    @Caching(put = @CachePut(value = "classify_detail", key = "'classify_detail' + #id"), evict = { @CacheEvict(value = {"queryClassifyPage", "queryClassifyList"}, allEntries = true)})
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void use(Long id) {
        Classify classify = classifyDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(classify)) {
            throw new BizException(ResponseCode.CLASSIFY_UN_EXIST, "分类不存在");
        }
        if(1 == classify.getStatus()) {
            throw new BizException(ResponseCode.CLASSIFY_STATUS_ERROR, "分类状态错误");
        }
        classify.setStatus(1);
        classifyDAO.updateByPrimaryKeySelective(classify);
    }

    /**
     * 停用
     * @param id
     */
    @Caching(put = @CachePut(value = "classify_detail", key = "'classify_detail' + #id"), evict = { @CacheEvict(value = {"queryClassifyPage", "queryClassifyList"}, allEntries = true)})
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void cancel(Long id) {
        Good good = new Good();
        good.setClassifyId(id);
        List<Good> goods = goodDAO.select(good);
        if(null != goods) {
            throw new BizException(ResponseCode.CLASSIFY_UN_EXIST, "分类下存在商品，不能停用");
        }
        Classify classify = classifyDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(classify)) {
            throw new BizException(ResponseCode.CLASSIFY_UN_EXIST, "分类不存在");
        }
        if(2 == classify.getStatus()) {
            throw new BizException(ResponseCode.CLASSIFY_STATUS_ERROR, "分类状态错误");
        }
        classify.setStatus(2);
        classifyDAO.updateByPrimaryKeySelective(classify);
    }

    /**
     * 详情
     * @param id
     */
    @Cacheable(value = "classify_detail", key = "'classify_detail' + #id")
    public Classify detail(Long id) {
        Classify Classify = classifyDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(Classify)) {
            throw new BizException(ResponseCode.CLASSIFY_UN_EXIST, "Classify不存在");
        }
        return Classify;
    }

    @CacheEvict(value = {"queryClassifyPage", "queryClassifyList", "classify_detail"}, allEntries = true)
    public void clear() {
    }

}
